#include "MenuSystem.h"
#include "IrControl.h"
#include <fcntl.h>
#include "Menu/Tool/frame.h"


#define ITEM_TEST_SOUND_LEFT	(0)
#define ITEM_TEST_SOUND_RIGHT	(1)
#define ITEM_TEST_ESC	(2)

#define TEST_SOUND_BUTTON_QTY   (3)


TPosCoor	TestSoundItemPos[TEST_SOUND_BUTTON_QTY] = { {180,164}, {460,164},{ 577,380 }};
TRect		TestSoundTouchRect[TEST_SOUND_BUTTON_QTY];
int			TestSoundFocus = -1;
TFrame			TestSoundFrame;
char		*TestSoundInfoStr[TEST_SOUND_BUTTON_QTY] = {"LEFT","RIGHT","ESC"};
extern unsigned char rece_handle;	
extern unsigned char dtpresent_flag;
extern pthread_cond_t dt_cond;



int TestSoundGetTouchID( TPosCoor pos )
{
	int i;		
	
		for( i = 0; i < TEST_SOUND_BUTTON_QTY; i++ )
			if( ut_PtInRect( pos, TestSoundTouchRect[i] ) )
				{				
				return i;
				}
		
	return -1;
}



void TestSoundDrawItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{
	TJpeg pJpeg;
	TPosCoor pos;
	char FileName[20];
	int GetJpegIdx;

	if( idx < 0 || idx >= TEST_SOUND_BUTTON_QTY )
		return;

	switch (idx)
		{
			case 		ITEM_TEST_SOUND_LEFT:
			case 		ITEM_TEST_SOUND_RIGHT:
				if( bfocus ){
				GetJpegIdx = IMG_TEST_SOUND_ITEM_FOCUS_LEFT_RIGHT;
				}else{
				GetJpegIdx = IMG_TEST_SOUND_ITEM_LEFT_RIGHT ;
				}
				break;
			case		ITEM_TEST_ESC:
				if( bfocus ){
				GetJpegIdx = IMG_TEST_SOUND_ITEM_FOCUS_ESC ;
				}else{
				GetJpegIdx = IMG_TEST_SOUND_ITEM_ESC ;
				}
				break;
		}
	
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_WHITE, 0, 0 );
	pos = TestSoundItemPos[idx];
	sprintf( FileName, "%d/%d.jpg", TEST_SOUND_FOLDER, GetJpegIdx);	
	pJpeg = GetJpegPtr(FileName);
	DrawJpeg(pCanvas, pos.x, pos.y, FileName);
	TestSoundTouchRect[idx].left = pos.x;
	TestSoundTouchRect[idx].right = pos.x + pJpeg.Width;
	TestSoundTouchRect[idx].top = pos.y;
	TestSoundTouchRect[idx].bottom = pos.y + pJpeg.Height;
	g_Puts( pCanvas, FONT33, TestSoundTouchRect[idx], TEXT_ALIGN_CENTER, TestSoundInfoStr[idx] );
	
}

void TestSoundDraw( BOOL bDrawAll )
{
	int i;
	
	//g_FillRect( TestSoundFrame.pCanvas, 0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, RGB_BLACK );
	DrawJpeg(TestSoundFrame.pCanvas, 0, 0, "/logo.jpg" );
	for( i = 0; i < TEST_SOUND_BUTTON_QTY; i++ )
		TestSoundDrawItem( TestSoundFrame.pCanvas, i, FALSE );
		
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	//dt_Present();
}



BOOL TestSoundBtnEnter(int idx)
{

	if (idx <0)
		return TRUE;
	TestSoundDrawItem( TestSoundFrame.pCanvas, idx, FALSE );
	switch(idx)
	{
		case ITEM_TEST_ESC:		
		case ITEM_TEST_SOUND_LEFT:
		case ITEM_TEST_SOUND_RIGHT:
			TestMainInit(1);
			dtpresent_flag++;		
			pthread_cond_signal(&dt_cond);
			break;		
	}
	LOG( "btn = %d\n", idx );	

	return TRUE;
}


void TestSoundInit()
{
	ir_SetCurrentID( IR_TEST_SOUND);
	frm_Create( &TestSoundFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );
	TestSoundDraw( TRUE );
	printf ("in test main Init ~~\n");
	
}


void TestSoundInput(UInt32 key)
{

	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;
		TPosCoor pos;
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				if( ( TestSoundFocus = TestSoundGetTouchID( pos )) != -1 )
				{
					TestSoundDrawItem( TestSoundFrame.pCanvas, TestSoundFocus, TRUE );
					dt_Present_part(TestSoundTouchRect [TestSoundFocus]);
				}
			}
		}
		else
		{
			if( ( focus = TestSoundGetTouchID( pos ) ) == TestSoundFocus )
				TestSoundBtnEnter( TestSoundFocus );
			else if( TestSoundFocus != -1 )
			{
				TestSoundDrawItem( TestSoundFrame.pCanvas, TestSoundFocus, FALSE );
				dt_Present_part(TestSoundTouchRect [TestSoundFocus]);
			}
			TestSoundFocus = -1;
		}
		LastKey = key;//hangsheng add 071126
	}
	
}


